/*
Main analysis replication material for:
"Personality and Roots of Welfare State Support: How Openness to Experience Moderates the Influence of Self-Interest and Ideology on Redistributive Preferences"

Authors: Tobias Heide-Jørgensen, Peter Thisted Dinesen, and Kim Mannemar Sønderskov.

PURPOSE: Reproducing Danish results.
*/

*Descriptive statistics
use "Main_data_wave1-3.dta",clear

foreach var of varlist open_group open_avg_group open_w2_group cons_group emo_group agree_group extrav_group mar {
tab `var',gen(`var')
}

eststo desk: estpost sum redis inc ideo open open_group1 open_group2 open_group3 open_avg open_avg_group1 open_avg_group2 open_avg_group3 open_w2 open_w2_group1 open_w2_group2 open_w2_group3 agree agree_group1 agree_group2 agree_group3 cons cons_group1 cons_group2 cons_group3 emo emo_group1 emo_group2 emo_group3 extrav extrav_group1 extrav_group2 extrav_group3 female age edu_year benefit immi mar1 mar2 mar3 mar4 h_inc unemp,d

esttab desk, cells("count mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p95(fmt(2))")

*Properties of personality measures
sum open if wave==2
sum open if wave==3
alpha open_w2_1 open_w2_2
alpha open_w3_1 open_w3_2
icc open pnr

sum agree if wave==2
sum agree if wave==3
alpha agree_w2_1 agree_w2_2
alpha agree_w3_1 agree_w3_2
icc agree pnr

sum cons if wave==2
sum cons if wave==3
alpha cons_w2_1 cons_w2_2
alpha cons_w3_1 cons_w3_2
icc cons pnr

sum emo if wave==2
sum emo if wave==3
alpha emo_w2_1 emo_w2_2
alpha emo_w3_1 emo_w3_2
icc emo pnr

sum extrav if wave==2
sum extrav if wave==3
alpha extrav_w2_1 extrav_w2_2
alpha extrav_w3_1 extrav_w3_2
icc extrav pnr

*Correlates of openness scale
foreach var of varlist trust pol_met pol_work pol_con pol_int pol_dis ideo left_vot {
pwcorr open `var' if wave==2,obs sig
pwcorr open_avg `var' if wave==2,obs sig
}

foreach var of varlist trust pol_met pol_work pol_con pol_dis left_vot {
pwcorr open `var' if wave==3,obs sig
pwcorr open_avg `var' if wave==3,obs sig
}

***INCOME ANALYSES***
use "Main_data_wave2-3.dta",clear

xtset pnr wave

*Absolute income changes between wave 2 and 3
sort pnr wave
by pnr: gen inc_lag=inc[_n-1]
gen inc_dif=inc-inc_lag
gen inc_dif_p= abs(inc_dif/inc_lag*100)
sum inc_dif_p,d

*Primary analyses
xtreg redis inc_ln, fe cluster(pnr)
estimates store inc_bi
	
xtreg redis inc_ln benefit edu_year open cons emo extrav agree i.wave, fe cluster(pnr) 
estimates store inc_con

xtreg redis c.inc_ln##c.open benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store inc_full

mylabels -0.3 (0.1) 0.1, format(%03.1f) local(inc_mar) myscale(@)
mylabels 0 (0.2) 1, format(%03.1f) local(open_inc) myscale(@)
	
margins, dydx(inc_ln) at(open=(0 (0.01) 1))
marginsplot, scheme(plotplain) level(95) recast(line) recastci(rarea) ciopts(fcolor(%20) acolor(%20) lcolor(white)) legend(off) ylabel(`inc_mar', angle(h) labsize(small)) yline(0, lp(solid) lc(gs13)) yscale(titlegap(*5)) xtitle(Openness) xscale(titlegap(*10)) ytitle(Estimated association with income) title("") addplot(hist open if e(sample)==1, discrete percent yaxis(2) yscale(alt axis(2) off) ylabel(0 100,axis(2) nolabels noticks) xlabel(`open_inc', labsize(small)) xscale(range(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1")) ytitle("",axis(2)) color(black) fi(inten100) barw(0.005) lcolor(none)) 

xtreg redis c.inc_ln##open_group benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store inc_full_nonlin

mylabels -0.3 (0.1) 0, format(%03.1f) local(inc_open_group) myscale(@)
	
xtreg redis c.inc_ln##open_group benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)

margins, dydx(inc_ln) at(open_group=(1 2 3))
marginsplot, scheme(plotplain) level(95) recast(scatter) plotopts(color(black) ms(o)) legend(off) ylabel(`inc_open_group', angle(h) labsize(small)) ytick(0.03,notick) yline(0,lpattern(solid) lc(gs13)) yscale(titlegap(*-15)) xtitle(Openness) xscale(titlegap(*10)) ytitle(Estimated association with income) title("") addplot(hist open_group if e(sample)==1, discrete barwidth(.02) percent yaxis(2) yscale(alt axis(2) off) ylabel(0 400,axis(2) nolabels noticks ) xlabel(1 "Low" 2 "Medium" 3 "High", labsize(small)) xscale(range(0.95 3.05)) ytitle("",axis(2)) color(black) fi(inten100) barw(0.005) lcolor(none)) 	

esttab inc_bi inc_con inc_full inc_full_nonlin, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Binning estimator
interflex redis inc_ln open benefit edu_year cons emo extrav agree, fe(pnr wave) cluster(pnr)

mat A = r(margeff)
mat B = r(estBin)
svmat A
svmat B

loc b1 = B1[1]
loc b2 = B1[2]
loc b3 = B1[3]

mylabels -0.3 (0.1) 0.1, format(%03.1f) local(inc_mar) myscale(@)
mylabels 0 (0.2) 1, format(%03.1f) local(open_inc) myscale(@)
tw (rarea A4 A5 A1, fcolor(%20) acolor(%20) lcolor(white)) (line A2 A1,lp(solid) lc(black)) (scatter B2 B1, mcolor(red) ms(o)) (rcap B4 B5 B1 , lc(red)) (hist open if e(sample)==1, discrete percent yaxis(2) yscale(alt axis(2) off) ylabel(0 100,axis(2) nolabels noticks) xlabel(`open_inc', labsize(small)) xscale(range(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1")) ytitle("",axis(2)) color(black) fi(inten100) lcolor(none) barw(0.005)) , legend(off) ylabel(`inc_mar', angle(h) labsize(small)) yline(0,lpattern(solid) lc(gs13)) yscale(titlegap(*5)) xtitle(Openness) xscale(titlegap(*10)) ytitle(Estimated association with income) title("") text(0.07 `b1' "L"0.07 `b2' "M" 0.07 `b3' "H",color(red)) scheme(plotplain)

drop A1-B5
				
*Average openness
xtreg redis c.inc_ln##c.open_avg benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr) 
estimates store inc_avg

xtreg redis c.inc_ln##open_avg_group benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store inc_avg_nonlin
	
*Openness wave 2
xtreg redis c.inc_ln##c.open_w2 benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store inc_w2

xtreg redis c.inc_ln##open_w2_group benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store inc_w2_nonlin

esttab inc_avg inc_avg_nonlin inc_w2 inc_w2_nonlin, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Controlling for lagged ideology
xtreg redis c.inc_ln##open_group benefit edu_year cons emo extrav agree i.wave c.ideo_lag, fe cluster(pnr)	
estimates store inc_ideolag_open_group
	
xtreg redis c.inc_ln##open_group benefit edu_year cons emo extrav agree i.wave c.ideo_lag##open_group, fe cluster(pnr)	
estimates store inc_ideolag_open_group_int

esttab inc_ideolag_open_group inc_ideolag_open_group_int, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label keep(inc_ln ideo_lag 2.open_group 3.open_group 2.open_group#c.inc_ln 3.open_group#c.inc_ln 2.open_group#c.ideo_lag 3.open_group#c.ideo_lag)

*Controlling for interaction with education
sum edu_year,d
gen edu_cent=edu_year-r(mean) 
		
xtreg redis c.inc_ln##open_group c.inc_ln##c.edu_cent benefit cons emo extrav agree i.wave, fe cluster(pnr) 
estimates store inc_open_edu

esttab inc_open_edu, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label 
	
*Controlling for interactions with other personality traits
xtreg redis c.inc_ln##open_group c.inc_ln##ib(3).cons_group c.inc_ln##ib(3).emo_group c.inc_ln##ib(3).extrav_group c.inc_ln##ib(3).agree_group benefit edu_year i.wave,fe cluster(pnr)
estimates store inc_other_pers_int

esttab inc_other_pers_int, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

xtreg redis c.inc_ln##open_group c.inc_ln##ib(3).cons_group c.inc_ln##ib(3).emo_group c.inc_ln##ib(3).extrav_group c.inc_ln##ib(3).agree_group benefit edu_year i.wave,fe cluster(pnr)

*Income analyses with other personality traits
xtreg redis c.inc_ln##i.cons_group open emo extrav agree benefit edu_year i.wave,fe cluster(pnr)
estimates store inc_cons

testparm cons_group#c.inc_ln 
return list
	
xtreg redis c.inc_ln##i.emo_group cons open extrav agree benefit edu_year i.wave,fe cluster(pnr)
estimates store inc_emo	
	
testparm emo_group#c.inc_ln
return list
	
xtreg redis c.inc_ln##i.extrav_group cons open emo agree benefit edu_year i.wave,fe cluster(pnr)
estimates store inc_extrav
	
testparm extrav_group#c.inc_ln
return list
	
xtreg redis c.inc_ln##i.agree_group cons open emo extrav benefit edu_year i.wave,fe cluster(pnr)
estimates store inc_agree	

testparm agree_group#c.inc_ln
return list
	
esttab inc_cons inc_emo inc_extrav inc_agree, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Household income
xtreg redis c.h_inc_ln##c.open benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store h_inc_open

xtreg redis c.h_inc_ln##open_group benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store h_inc_open_group
	
esttab h_inc_open h_inc_open_group, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Single openness items
xtreg redis c.inc_ln##c.filo_w2 benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr) 
estimates store inc_filo_w2

xtreg redis c.inc_ln##c.fanta_w2 benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr) 
estimates store inc_fanta_w2

esttab inc_filo_w2 inc_fanta_w2, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label replace

*Placebo analysis: Attitudes toward unemployed people
xtreg unemp c.inc_ln##c.open benefit edu_year cons emo extrav agree i.wave, fe cluster(pnr)
estimates store placebo_inc_open

esttab placebo_inc_open, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label
	
***IDEOLOGY ANALYSES***
use "Main_data_wave2.dta",clear

*Primary analyses
reg redis ideo,robust
estimates store ideo_bi

reg redis ideo benefit edu_year inc_ln female c.age##c.age i.mar immi open cons emo extrav agree,robust
estimates store ideo_con

reg redis c.ideo##c.open edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_full
	
mylabels -1 (0.2) 0, format(%03.1f) local(ideo_mar) myscale(@)
mylabels 0 (0.2) 1, format(%03.1f) local(open_ideo) myscale(@)
	
margins, dydx(ideo) at(open=(0 (.01) 1))
marginsplot, scheme(plotplain) level(95) recast(line) recastci(rarea) ciopts(fcolor(%25) acolor(%20) lcolor(white)) legend(off) ylabel(`ideo_mar', angle(h) labsize(small)) yline(0,lpattern(solid) lc(gs13)) yscale(titlegap(*-15)) xtitle(Openness) xscale(titlegap(*10)) ytitle("Estimated association with left-right position") title("") ytick(-1.1 0.07,notick) addplot(hist open if e(sample)==1, discrete percent yaxis(2) yscale(alt axis(2) off) ylabel(0 100,axis(2) nolabels noticks) xlabel(`open_ideo', labsize(small)) xscale(range(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1")) ytitle("",axis(2)) color(black) fi(inten100) barw(0.005) lcolor(none)) 

reg redis c.ideo##open_group edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_full_nonlin

esttab ideo_bi ideo_con ideo_full ideo_full_nonlin, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Binning estimator 
gen age_sq=age*age
tab mar,gen(mar)

interflex redis ideo open edu_year benefit inc_ln female age age_sq mar2 mar3 mar4 immi cons emo extrav agree,vce(robust)

mat A = r(margeff)
mat B = r(estBin)
svmat A
svmat B

loc b1 = B1[1]
loc b2 = B1[2]
loc b3 = B1[3]

mylabels -1.2 (0.2) 0, format(%03.1f) local(ideo_mar) myscale(@)
mylabels 0 (0.2) 1, format(%03.1f) local(open_ideo) myscale(@)
qui: reg redis ideo open edu_year benefit inc_ln female age age_sq mar2 mar3 mar4 immi cons emo extrav agree
tw (rarea A4 A5 A1, fcolor(%20) acolor(%20) lcolor(white)) (line A2 A1,lp(solid) lc(black)) (scatter B2 B1, mcolor(red) ms(o)) (rcap B4 B5 B1 , lc(red)) (hist open if e(sample)==1, discrete percent yaxis(2) yscale(alt axis(2) off) ylabel(0 100,axis(2) nolabels noticks) xlabel(`open_ideo', labsize(small)) xscale(range(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1")) ytitle("",axis(2)) color(black) fi(inten100) barw(0.005) lcolor(none))  , legend(off) ylabel(`ideo_mar', angle(h) labsize(small)) yline(0,lpattern(solid) lc(gs13)) yscale(titlegap(*5)) xtitle(Openness) xscale(titlegap(*10)) ytitle(Estimated association with left-right position) title("") scheme(plotplain) ytick(0.03,notick) text(-0.1 `b1' "L" -0.1 `b2' "M" -0.1 `b3' "H",color(red))

drop A1-B5

*Controlling for interaction with education
sum edu_year,d
gen edu_cent=edu_year-r(mean)
	
reg redis c.ideo##c.open c.ideo##c.edu_cent benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_open_edu
	
esttab ideo_open_edu, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Controlling for interactions with other personality traits
foreach var of varlist cons emo extrav agree { 
sum `var',d	
gen `var'_cent=`var'-r(mean)
}
	
reg redis c.ideo##c.open edu_year benefit inc_ln female c.age##c.age i.mar immi c.ideo##c.cons_cent c.ideo##c.emo_cent c.ideo##c.extrav_cent c.ideo##c.agree_cent,robust
estimates store ideo_other_pers

esttab ideo_other_pers, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label
	
*Controlling for lagged ideology
reg redis c.ideo##c.open ideo_lag edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_open_ideo_lag
	
reg redis c.ideo##c.open c.ideo_lag##c.open edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_open_ideo_lag_int

esttab ideo_open_ideo_lag ideo_open_ideo_lag_int, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Ideology analyses with other personality traits
reg redis c.ideo##c.cons edu_year benefit inc_ln female c.age##c.age i.mar immi open emo extrav agree,robust
estimates store ideo_cons

reg redis c.ideo##c.emo edu_year benefit inc_ln female c.age##c.age i.mar immi open agree extrav cons,robust
estimates store ideo_emo	

reg redis c.ideo##c.extrav edu_year benefit inc_ln female c.age##c.age i.mar immi open emo agree cons,robust
estimates store ideo_extrav		

reg redis c.ideo##c.agree edu_year benefit inc_ln female c.age##c.age i.mar immi open emo extrav cons,robust
estimates store ideo_agree	

esttab ideo_cons ideo_emo ideo_extrav ideo_agree, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label
	
reg redis c.ideo##c.agree edu_year benefit inc_ln female c.age##c.age i.mar immi open emo extrav cons,robust

mylabels -1.4 (0.2) -0.4, format(%03.1f) local(ideo_mar) myscale(@)
mylabels 0 (0.2) 1, format(%03.1f) local(agree_ideo) myscale(@)
	
margins, dydx(ideo) at(agree=(0 (.01) 1))
marginsplot, scheme(plotplain) level(95) recast(line) recastci(rarea) ciopts(fcolor(%25) acolor(%20) lcolor(white)) legend(off) ylabel(`ideo_mar', angle(h) labsize(small)) yline(0,lpattern(solid) lc(gs13)) yscale(titlegap(2)) xtitle(Agreeableness) xscale(titlegap(*10)) ytitle("Estimated association with left-right position") title("") ytick(-0.35,notick) addplot(hist agree if e(sample)==1, discrete percent yaxis(2) yscale(alt axis(2) off) ylabel(0 100,axis(2) nolabels noticks) xlabel(`agree_ideo', labsize(small)) xscale(range(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1")) ytitle("",axis(2)) color(black) fi(inten100) barw(0.005) lcolor(none)) 

*Single openness items
reg redis c.ideo##c.filo_w2 edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust 
estimates store ideo_filo_w2

reg redis c.ideo##c.fanta edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store ideo_fanta_w2

esttab ideo_filo_w2 ideo_fanta_w2, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Placebo analysis: Attitudes toward unemployed people
reg unemp c.ideo##c.open edu_year benefit inc_ln female c.age##c.age i.mar immi cons emo extrav agree,robust
estimates store placebo_ideo_open

esttab placebo_ideo_open, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label

*Using wave 1-2 and adding individual fixed effects
use "Main_data_wave1-2.dta",clear

xtset pnr round
	
xtreg redis c.ideo##c.open_w2 edu_year benefit inc_ln i.round, fe cluster(pnr)
estimates store ideo_fe
		
xtreg redis c.ideo##open_w2_group edu_year benefit inc_ln i.round, fe cluster(pnr)
estimates store ideo_fe_group

esttab ideo_fe ideo_fe_group, b(2) r2 se star(* 0.10 ** 0.05 *** 0.01) stats(N_g N, fmt(0)) label